package cn.edu.scau.nursingcenter.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.filter.FormContentFilter;
import org.springframework.web.filter.HttpPutFormContentFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import cn.edu.scau.nursingcenter.interceptor.LoginInterceptor;




/**
 *  java配置类OfficeConfig：针对项目的全局配置
 * @author feng
 *
 */
@SpringBootConfiguration
public class OfficeConfig extends WebMvcConfigurationSupport{
//	@Autowired
//	private MyDateConverter myDateConverter;
	@Autowired
	private LoginInterceptor loginInterceptor;
	
	/**
	 * 拦截器代码，允许登录请求，以及静态资源、swagger、druid通过
	 */
	@Override
	protected void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(loginInterceptor)//注册拦截器
			.addPathPatterns("/**")//拦截所有用户请求
			.excludePathPatterns("/webjars/**"
					,"/login/**","/check_user","/exit","/check_amdin"
					,"/swagger-ui.html/**","/v2/**","/swagger-resources/**"
					,"/druid/**");//例外：不拦截的请求
		super.addInterceptors(registry);
	}
	
	
	/**
	 * 解决PUT请求无法获取书的处理
	 * @return
	 */
	@Bean
	public FormContentFilter FormContentFilter() {
		return new FormContentFilter();
	}
	

	
	/**
	 * 注册转换器
	 */
//	@Override
//	protected void addFormatters(FormatterRegistry formatterRegistry) {
//		formatterRegistry.addConverter(myDateConverter);
//		super.addFormatters(formatterRegistry);
//	}

	/**
	 * 	资源映射
	 */
	@Override
	protected void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/**")
			.addResourceLocations("classpath:/META-INF/resources/")
			.addResourceLocations("classpath:/resources/")
			.addResourceLocations("classpath:/public/")
			.addResourceLocations("classpath:/static/")
			.addResourceLocations("classpath:/templates/");
		registry.addResourceHandler("/webjars/**").addResourceLocations("/webjars/").resourceChain(false);
		
		super.addResourceHandlers(registry);
	}


	@Override
	protected void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**")
			.allowedOrigins("*")
			.allowCredentials(true)
			.allowedMethods("GET","POST","PUT","DELETE","PATCH","OPTIONS","HEAD")
			.maxAge(3600*48);
		super.addCorsMappings(registry);
	}
	
}
